package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.entity.TeacherEntity;
import com.hy.admin.helper.request.CourseDownloadRequest;
import com.hy.admin.helper.request.PersonalRequest;
import com.hy.admin.helper.request.TeacherRequest;
import com.hy.admin.helper.response.PersonalResponse;
import com.hy.admin.service.PersonalService;
import com.hy.admin.service.TeacherService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by yangqq on 18/4/3.
 * 个人中心
 */
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    protected final Logger logBase = LoggerFactory.getLogger(TeacherController.class);
    @Autowired
    private TeacherService teacherService;

    /**
     * 教师列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponseDTO list(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("teacher_list_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        TeacherRequest teacherRequest = gson.fromJson(apiRequestDTO.getRequestData(), TeacherRequest.class);
        ServicesResult<List<TeacherEntity>> teacherResponse = teacherService.list(teacherRequest);
        apiResponseDTO.initServicesResult(teacherResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(teacherResponse.getResult()));
        logBase.info("teacher_list_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 名师推荐
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.POST)
    public ApiResponseDTO recommend(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("teacher_recommend_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        TeacherRequest teacherRequest = gson.fromJson(apiRequestDTO.getRequestData(), TeacherRequest.class);
        ServicesResult<List<TeacherEntity>> teacherResponse = teacherService.recommend(teacherRequest);
        apiResponseDTO.initServicesResult(teacherResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(teacherResponse.getResult()));
        logBase.info("teacher_recommend_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 名师推荐
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/home", method = RequestMethod.POST)
    public ApiResponseDTO home(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("teacher_home_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        TeacherRequest teacherRequest = gson.fromJson(apiRequestDTO.getRequestData(), TeacherRequest.class);
        ServicesResult<PersonalResponse> teacherResponse = teacherService.home(teacherRequest.getTeacherId());
        apiResponseDTO.initServicesResult(teacherResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(teacherResponse.getResult()));
        logBase.info("teacher_home_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 后台教师列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/teacherList", method = RequestMethod.POST)
    public ApiResponseDTO teacherList(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("teacher_teacherList_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        TeacherRequest teacherRequest = gson.fromJson(apiRequestDTO.getRequestData(), TeacherRequest.class);
        ServicesResult<List<MemberEntity>> teacherResponse = teacherService.listByType(teacherRequest);
        apiResponseDTO.initServicesResult(teacherResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(teacherResponse.getResult()));
        logBase.info("teacher_teacherList_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 教师审核
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public ApiResponseDTO audit(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("teacher_audit_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        TeacherRequest teacherRequest = gson.fromJson(apiRequestDTO.getRequestData(), TeacherRequest.class);
        ServicesResult<Boolean> teacherResponse = teacherService.audit(teacherRequest);
        apiResponseDTO.initServicesResult(teacherResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(teacherResponse.getResult()));
        logBase.info("teacher_audit_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
